package com.lyloou.practiceredis.service.impl;

import com.lyloou.practiceredis.service.RedisStringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author lyloou
 * @date 2021/01/27
 * @desc
 */
@Service
public class RedisStringServiceImpl implements RedisStringService {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public void put(String key, String value, int timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    @Override
    public String get(String key) {
        final Object value = redisTemplate.opsForValue().get(key);
        if (value == null) {
            return null;
        }

        return String.valueOf(value);
    }
}
